/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QMessageBox>

#include "dlgsettimer.h"
#include "qtvomp.h"

DlgSetTimer::DlgSetTimer( MainWindow* mainWindow, TimerEntry timer, DlgSetTimer::EditType type, QWidget * parent, Qt::WindowFlags flags )
    : QDialog( parent, flags ),
    m_mainWindow( mainWindow ),
    m_timer( timer ),
    m_oldTimer( timer ),
    m_type ( type )
{
    initUi();

    connect( this,                SIGNAL( setTimer( TimerEntry ) ),
             m_mainWindow->vdr(), SLOT( setTimer( TimerEntry ) ) );
    connect( this,                SIGNAL( editTimer( TimerEntry, TimerEntry ) ),
             m_mainWindow->vdr(), SLOT( editTimer( TimerEntry, TimerEntry ) ) );
}


DlgSetTimer::~DlgSetTimer()
{
}


void DlgSetTimer::initUi()
{
    ui.setupUi( this );

    if ( m_type == New )
    {
        setWindowTitle( tr( "Set timer" ) );
    }
    else
    {
        setWindowTitle( tr( "Edit timer" ) );
    }

    if ( m_mainWindow->channelList()->size() > 0 )
    {
        int row = m_mainWindow->channelList()->rowFromChannel( m_timer.channelNumber );
        if ( row != -1 )
        {
            ui.channel->setText( QString().setNum( m_timer.channelNumber ) +
                                " - " +
                                m_mainWindow->channelList()->at( row ).name );
        }
        else //The channel number is not existing, only radio or tv was loaded, but the other is needed
        {
            ui.channel->setText( QString().setNum( m_timer.channelNumber ) );
        }
    }
    else //No channels loaded yet
    {
        ui.channel->setText( QString().setNum( m_timer.channelNumber ) );
    }

    ui.startDate->setDateTime( m_timer.startTime );
    ui.startTime->setDateTime( m_timer.startTime );

    ui.stopDate->setDateTime( m_timer.stopTime );
    ui.stopTime->setDateTime( m_timer.stopTime );

    ui.name->setText( m_timer.file );

    ui.priority->setText( QString().setNum( m_timer.priority ) );
    ui.lifeTime->setText( QString().setNum( m_timer.lifeTime ) );

    if ( m_timer.active )
    {
        ui.active->setChecked( true );
    }

    if ( m_timer.pending )
    {
        ui.pending->setChecked( true );
    }

    if ( m_timer.recording )
    {
        ui.recording->setChecked( true );
    }

    ui.buttonCancel->setFocus();
}


void DlgSetTimer::accept()
{
    qDebug() << "DlgSetTimer::accept() called";
    m_timer.startTime.setDate( ui.startDate->date() );
    m_timer.startTime.setTime( ui.startTime->time() );
    m_timer.stopTime.setDate( ui.stopDate->date() );
    m_timer.stopTime.setTime( ui.stopTime->time() );
    m_timer.priority = ui.priority->text().toInt();
    m_timer.lifeTime = ui.lifeTime->text().toInt();

    if ( ( m_oldTimer == m_timer ) && ( m_type == Edit ) )
    {
        qDebug() << "DlgSetTimer::accept: timer is unchanged";
        QDialog::accept();
        return;
    }

    if ( m_timer.startTime >= m_timer.stopTime )
    {
        QMessageBox::warning( this,
                              tr( "Wrong times" ),
                              tr( "Start time is after stop time!"),
                              QMessageBox::Ok );
    }
    else
    {
        int recordingHours = m_timer.startTime.secsTo( m_timer.stopTime ) / 60 / 60;
        if ( recordingHours > 6 ) // > 6 hours
        {
            if ( QMessageBox::question( this, tr( "Long recording" ),
                                        tr( "Recording woud be longer than %1 hours.\n\nDo you want to record?" ).arg( recordingHours ),
                                        QMessageBox::Yes | QMessageBox::No,
                                        QMessageBox::No ) == QMessageBox::Yes )
            {
                if ( m_type == New )
                {
                    emit setTimer( m_timer );
                }
                else
                {
                    qDebug() << "1";
                    emit editTimer( m_oldTimer, m_timer );
                }
                QDialog::accept();
            }
        }
        else
        {
            if ( m_type == New )
            {
                emit setTimer( m_timer );
            }
            else
            {
                emit editTimer( m_oldTimer, m_timer );
            }
            QDialog::accept();
        }
    }
}


